package com.jack006.stock.service.impl;

import com.jack006.pojo.StockNews;
import com.jack006.pojo.bo.StockNewsBO;
import com.jack006.pojo.bo.StockNewsDataBO;
import com.jack006.stock.mapper.StockNewsMapper;
import com.jack006.stock.service.StockNewsService;
import com.jack006.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Classname StockNewsServiceImpl
 * @Description 股票新闻实现类
 * @Date 2021-07-18 20:21
 * @Created by jack
 */
@Service
public class StockNewsServiceImpl implements StockNewsService {

    final static Logger logger = LoggerFactory.getLogger(StockNewsServiceImpl.class);


    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private StockNewsMapper stockNewsMapper;

    @Transactional
    @Override
    public void searchStockNews(Long timeStampCurrent,Integer tagIndex,Integer pageSize) {
        List<StockNewsDataBO> remoteNews = getRemoteNews(timeStampCurrent, tagIndex, pageSize);
        if (!remoteNews.isEmpty()) {
            List<StockNews> newsList = new ArrayList<>();
            for (StockNewsDataBO remoteNew : remoteNews) {
                StockNews news = new StockNews();
                news.setId(remoteNew.getId());
                news.setTitle(remoteNew.getTitle());
                news.setContent(remoteNew.getContent());
                news.setLevel(remoteNew.getLevel());
                Date publishTime = DateUtil.TimestampToDate(remoteNew.getTime());
                news.setPublishTime(publishTime);
                news.setCreateTime(new Date());
                news.setDelFlag(1);
                news.setHotNum(remoteNew.getHot_num());
                news.setNewsType(remoteNew.getNews_type());
                List<String> related_stocks = remoteNew.getRelated_stocks();
                if (!related_stocks.isEmpty()) {
                    news.setRelatedStocks(related_stocks.toString());
                }
                newsList.add(news);
            }
            try{
                stockNewsMapper.batchInsert(newsList);
            }catch (Exception e) {
                logger.info(e.getMessage());
            }
        }
    }

    private List<StockNewsDataBO> getRemoteNews(long seqMark, int tagIndex, int pageSize) {
        String flushServerUrlExecute = "https://wechatapp.futunn.com/news/wechat/flash?prefer_lang=2" +
                "&tag_index="+tagIndex+"&seq_mark="+seqMark+"&page_size="+pageSize;
        ResponseEntity<StockNewsBO> responseEntity
                = restTemplate.getForEntity(flushServerUrlExecute,  StockNewsBO.class);
        StockNewsBO body = responseEntity.getBody();
        return body.getData().getNews();
    }


}
